package com.jamal2362.apktool.task;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.files.Refreshable;
import com.jamal2362.apktool.util.FileUtils;
import com.jamal2362.apktool.util.MUtil;
import com.jamal2362.apktool.util.Settings;
import com.jamal2362.apktool.util.TextUtil;
import com.jamal2362.apktool.util.UnApkm;

public class UnApkmTask extends AsyncTask<File, String, Boolean> {
	private Context context;
	private Refreshable refresh;
	private AlertDialog progress;
	private List<String> error;
	private boolean redirect;

	public UnApkmTask(Context ctx, Refreshable r) {
		this.context = ctx;
		this.refresh = r;
		this.error = new ArrayList<String>();
		this.redirect = false;
	}

	@Override
	protected void onPreExecute() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.search_progress, null);
		TextView tview = view.findViewById(R.id.searchprogressTextView1);
		tview.setText(R.string.mplease_wait);
		progress = new AlertDialog.Builder(context)
			.setView(view)
			.setCancelable(false)
			.create();
		progress.show();
	}

	@Override
	protected Boolean doInBackground(File[] p1) {
		for (File f : p1) {
			try {
				new UnApkm(context, f.getAbsolutePath(), getOutFile(f));
			} catch (Exception ex) {
				error.add(Log.getStackTraceString(ex));
			}
		}
		return true;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		refresh.refresh();
		progress.cancel();
		if (!error.isEmpty()) {
			String err = MUtil.strJoin(error, "<br>");
			Runnable copy = new Runnable()
			{
				@Override
				public void run() {
					TextUtil.setClipboard(context, MUtil.strJoin(error, "\n"));
				}
			};
			MUtil.createDialog(context, context.getString(R.string.error) + ": " + err, R.string.copy, copy);
		} else if (redirect) {
			Runnable go = new Runnable()
			{
				@Override
				public void run() {
					refresh.refresh(new File(Settings.output_directory));
				}
			};
			MUtil.createDialog(context, context.getString(R.string.complete) + "!", R.string.go_to, go);
		}
	}

	private String getOutFile(File f) {
		String dir = f.getParent();
		boolean noaccess = !f.getParentFile().canWrite();
		if (noaccess) {
			dir = Settings.output_directory;
			if (dir == null) {
				TextUtil.st(context, context.getString(R.string.output_directory_not_set));
				return null;
			}
			File d =  new File(dir);
			if (!d.exists() && !d.mkdirs()) {
				TextUtil.st(context, context.getString(R.string.output_directory_not_extsts, dir));
				return null;
			}
			if (!d.isDirectory()) {
				TextUtil.st(context, context.getString(R.string.not_directory, dir));
				return null;
			}
			redirect = true;
		}
		String[] name = FileUtils.parseFileName(f.getName());
		return dir + File.separator + FileUtils.genName(context, dir, name[0], ".apks", 0);
	}
}
